package com.hotelsystem.reservation.controllers;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.hotelsystem.reservations.layouts.RoomListView;
import com.hotelsystem.reservations.layouts.RoomView;
import com.hotelsystem.reservations.main.MainMenuController;
import com.hotelsystem.reservations.main.MainWindow;
import com.hotelsystem.reservations.rooms.Room;
import com.hotelsystem.reservations.rooms.RoomManagement;
import com.hotelsystem.reservations.rooms.RoomTypesEnum;

public class RoomListController {

	private ArrayList<Room> rooms;
	private RoomListView roomListView;
	private RoomManagement roomManager;
	private JPanel roomPanel;
	private MainWindow mainWindow;

	public RoomListController(MainMenuController mainMenuController) {
		roomManager = new RoomManagement();
		this.roomListView = new RoomListView();
		rooms = roomManager.getRooms();

		mainWindow = mainMenuController.getMainWindow();
		addListenerRoomView();
		addListenerRoomDelete();
	}

	public RoomManagement getRoomManager() {
		return roomManager;
	}

	public void setRoomManager(RoomManagement roomManager) {
		this.roomManager = roomManager;
	}

	public ArrayList<Room> getRooms() {
		return rooms;
	}

	public void setRooms(ArrayList<Room> rooms) {
		this.rooms = rooms;
	}

	public RoomListView getRoomListView() {
		return roomListView;
	}

	public void setRoomListView(RoomListView roomListView) {
		this.roomListView = roomListView;
	}

	public void fillList() {
		roomListView.setRoomsList(rooms);
	}

	private void addListenerRoomDelete() {

		roomListView.addListenerForDelete(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				int roomIndex = roomListView.getRoomsList().getSelectedIndex();
				if (roomIndex != -1) {
					Room room = rooms.get(roomIndex);
					rooms.remove(room);
					roomListView.setRoomsList(rooms);

					roomManager.deleteRoom(room);
					mainWindow.getMainFrame().getContentPane().invalidate();
					mainWindow.getMainFrame().getContentPane().validate();
				}
			}
		});

	}

	public void addListenerRoomView() {
		roomListView.addListenerForOpen(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				int roomIndex = roomListView.getRoomsList().getSelectedIndex();

				Room room = rooms.get(roomIndex);
				RoomView roomView = new RoomView(room);

				mainWindow.setJPanel(roomView.getMainPanel());
			}
		});
	}


	public JPanel getRoomListPanel() {
		return roomListView.getMainPanel();
	}



}
